home *** CD-ROM | disk | FTP | other *** search
- /*
- Sound Asleep
- ⌐1992 David Thompson
-
- A little hack to audibly let the user know when the PowerBook is either going
- to sleep or waking up.
-
- This source was hacked out of the Sample.c program that comes with MPW.
- There is also an assembler portion to this code. This source is free by the way!
- */
-
- /* You don't need all of these #includes. I just put them in to get going faster. */
- #include <Values.h>
- #include <Types.h>
- #include <Resources.h>
- #include <QuickDraw.h>
- #include <Fonts.h>
- #include <Events.h>
- #include <Windows.h>
- #include <Menus.h>
- #include <TextEdit.h>
- #include <Dialogs.h>
- #include <Desk.h>
- #include <ToolUtils.h>
- #include <Memory.h>
- #include <SegLoad.h>
- #include <Files.h>
- #include <OSUtils.h>
- #include <OSEvents.h>
- #include <DiskInit.h>
- #include <Packages.h>
- #include <Traps.h>
- #include <Power.h>
- #include <GestaltEqu.h>
- #include <Sound.h>
-
- #pragma segment Main
-
- /* C prototypes */
- Boolean DoEvent(EventRecord *event);
- Boolean DoMenuCommand(long menuResult);
- Boolean Initialize(void);
-
- /* Assembler routine declarations */
- extern pascal void soundHandleSetup(Handle yawnHandle, Handle wakeHandle);
- extern pascal long soundasleep();
- extern void _DataInit();
-
- /* Some defines */
- #define rMenuBar 128 /* application's menu bar */
- #define rAboutAlert 128 /* about alert */
-
- #define mApple 128 /* Apple menu */
- #define iAbout 1
-
- #define mFile 129 /* File menu */
- #define iQuit 1
-
- #define mEdit 130 /* Edit menu */
-
- /* Some globals */
- SleepQRec myRec;
- Handle yawnHndl = nil, wakeHndl = nil;
-
- main()
- {
- EventRecord event;
- Boolean keepgoing = false;
-
- UnloadSeg((Ptr) _DataInit); /* note that _DataInit must not be in Main! */
-
- MaxApplZone(); /* expand the heap so code segments load at the top */
-
- keepgoing = Initialize(); /* initialize the program */
- UnloadSeg((Ptr) Initialize); /* note that Initialize must not be in Main! */
-
- while (keepgoing)
- {
- SystemTask();
- if (GetNextEvent(everyEvent, &event))
- {
- keepgoing = DoEvent(&event);
- }
- }
- ExitToShell();
- }
-
-
- /* Do the right thing for an event. Determine what kind of event it is, and call
- the appropriate routines.
- We only really care about menu events.
- */
-
- Boolean DoEvent(event)
- EventRecord *event;
- {
- WindowPtr window;
- char key;
- Boolean keepgoing;
-
- switch (event->what)
- {
- case mouseDown:
- if (FindWindow(event->where, &window) == inMenuBar)
- {
- keepgoing = DoMenuCommand(MenuSelect(event->where));
- }
- break;
- case keyDown:
- case autoKey: /* check for menukey equivalents */
- key = event->message & charCodeMask;
- if (event->modifiers & cmdKey) /* Command key down */
- {
- if (event->what == keyDown)
- {
- keepgoing = DoMenuCommand(MenuKey(key));
- }
- }
- break;
- default:
- keepgoing = true;
- break;
- }
- return keepgoing;
- } /*DoEvent*/
-
-
- Boolean DoMenuCommand(menuResult)
- long menuResult;
- {
- short menuID; /* the resource ID of the selected menu */
- short menuItem; /* the item number of the selected menu */
- short itemHit;
- Str255 daName;
- short daRefNum;
- Boolean handledByDA;
- Boolean keepgoing = true;
-
- menuID = HiWord(menuResult); /* use macros for efficiency to... */
- menuItem = LoWord(menuResult); /* get menu item number and menu number */
- switch ( menuID ) {
- case mApple:
- switch ( menuItem ) {
- case iAbout: /* bring up alert for About */
- itemHit = Alert(rAboutAlert, nil);
- break;
- default: /* all non-About items in this menu are DAs */
- /* type Str255 is an array in MPW 3 */
- GetItem(GetMHandle(mApple), menuItem, daName);
- daRefNum = OpenDeskAcc(daName);
- break;
- }
- break;
- case mFile:
- switch ( menuItem ) {
- case iQuit:
- SleepQRemove(&myRec);
- keepgoing = false;
- break;
- }
- break;
- case mEdit: /* call SystemEdit for DA editing & MultiFinder */
- handledByDA = SystemEdit(menuItem-1); /* since we don╒t do any Editing */
- break;
- }
- HiliteMenu(0); /* unhighlight what MenuSelect (or MenuKey) hilited */
- return keepgoing;
- } /*DoMenuCommand*/
-
- /* Set up the world.
- Make sure we're running on a PowerBook or MacPortable on a System that has Gestalt.
- Set up the menus so the user can Quit when he wants to.
- We put this in it's own segment so we can unload it afterwards. */
- #pragma segment Initialize
- Boolean Initialize()
- {
- InitGraf((Ptr) &qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(nil);
- InitCursor();
-
- if (NGetTrapAddress(_GestaltDispatch, ToolTrap) != GetTrapAddress(_Unimplemented))
- {
- long machineType;
-
- if (!Gestalt(gestaltMachineType, &machineType))
- {
- if (machineType == gestaltPortable ||
- machineType > gestaltMacLC) // PowerBooks (and Quadras (need to fix this))
- {
- Handle menuBar;
-
- /* Set up the menu bar */
- menuBar = GetNewMBar(rMenuBar); /* read menus into menu bar */
- SetMenuBar(menuBar); /* install menus */
- DisposHandle(menuBar);
- AddResMenu(GetMHandle(mApple), 'DRVR'); /* add DA names to Apple menu */
- DrawMenuBar();
-
- /* Get the yawn and wakeup sound resources. */
- wakeHndl = GetResource(soundListRsrc, 129);
- if (wakeHndl != nil)
- {
- DetachResource(wakeHndl);
- HLock(wakeHndl);
- yawnHndl = GetResource(soundListRsrc, 128);
- if (yawnHndl == nil)
- {
- HUnlock(wakeHndl);
- DisposHandle(wakeHndl);
- return false;
- }
- DetachResource(yawnHndl);
- HLock(yawnHndl);
- }
- else
- {
- return false;
- }
-
- /* tell the assembler routine about our sound handles */
- soundHandleSetup(yawnHndl, wakeHndl);
-
- /* install our assembler routine in the sleep process queue */
- myRec.sleepQLink = 0;
- myRec.sleepQType = slpQType;
- myRec.sleepQProc = (ProcPtr) &soundasleep;
- myRec.sleepQFlags = 0;
-
- SleepQInstall(&myRec);
- return true;
- }
- }
- }
-
- /* If we get here, something is wrong so put up the About box which tells the user what
- machine they can run on. Certainly the error reporting can be improved, so if it really
- bothers you, feel free. */
- (void) Alert(rAboutAlert, nil);
- return false;
- } /*Initialize*/
-
- #pragma segment Main
-
- /* This routine is called at both sleep and wakeup time to play the appropriate sound.
- We want the wakeup sound to be asynchronous, but the beddie-bye sound to be synchronous,
- so we do a stupid trick on a static Boolean variable to toggle the sync state each time
- we're called since for every sleep there is a wake after it.
- */
-
- pascal void
- playthatsound(soundHndl)
- Handle soundHndl;
- {
- static Boolean async = true;
- async = !async;
- SndPlay(nil, soundHndl, async);
- }
-